#!/usr/bin/env bash

set -o errexit
set -o xtrace

#-- nova db setup
sudo nova-manage db sync

sudo ifconfig eth0 192.168.168.69 up
sudo route add default gw 192.168.168.1
	
# start nova and glance services

#what's our IP?
IP=`ip a show dev eth0 | grep 'inet ' | awk '{print $2}' | cut -d\/ -f1`
sudo sed -i -e "s/XXXIPXXX/$IP/g" /etc/nova/nova.conf

#do we support KVM?
if ! sudo kvm-ok 1>/dev/null 2>&1; then
sudo sed -i -e "s/kvm/qemu/" /etc/nova/nova-compute.conf
fi

#-- nova services
sudo start nova-api
sudo start nova-compute
sudo start nova-network
sudo start nova-scheduler
sudo start nova-objectstore

#-- glance services
sudo start glance-api
sudo start glance-registry

sudo nova-manage user admin user1
sudo nova-manage project create project1 user1
sudo nova-manage network create public 172.20.1.0/24 1 128 --bridge_interface eth0
sudo nova-manage project zipfile project1 user1
unzip nova.zip
source ./novarc
euca-add-keypair mykey > mykey.pem
chmod 600 mykey.pem
cloud-publish-tarball /srv/ttylinux-uec-amd64-12.1_2.6.35-22_1.tar.gz bucket1 amd64
IMAGE=`euca-describe-images | grep machine | cut -f2 | head -n1`
euca-run-instances $IMAGE -k mykey -t m1.tiny
euca-authorize -P icmp -t -1:-1 default
euca-authorize -P tcp -p 22 default

exit 0
